<template>
  <div class="appLeftMenuCont">
    <div
      class="appLeftMenuItem"
      :class="{ menuActive: commonStore.activedMenu === 'home' }"
      @click="changeMenu('home', '/home/recommend')"
    >
      发现音乐
    </div>
    <div
      class="appLeftMenuItem"
      :class="{ menuActive: commonStore.activedMenu === 'focus' }"
      @click="changeMenu('focus')"
    >
      关注
    </div>
    <div
      class="appLeftMenuItem"
      :class="{ menuActive: commonStore.activedMenu === 'fm' }"
      @click="changeMenu('fm')"
    >
      私人FM
    </div>
    <template v-if="loginStore.loginSuccess">
      <div class="gap">我的音乐</div>
      <div
        class="appLeftMenuItem"
        :class="{ menuActive: commonStore.activedMenu === 'myFavo' }"
        @click="gotoMyFavoList('myFavo')"
      >
        我喜欢的音乐
      </div>

      <el-collapse v-model="activeName" @change="handleCollapseChange">
        <el-collapse-item title="创建的歌单" name="1">
          <div
            v-for="item in userMusicSource.myCreatedPlaylist"
            class="appLeftMenuItem"
            :key="item.id"
            :class="{ menuActive: commonStore.activedMenu === String(item.id) }"
            @click="handleClickUserMusicSource(item.id)"
          >
            {{ item.name }}
          </div>
        </el-collapse-item>
        <el-collapse-item title="收藏的歌单" name="2">
          <div
            v-for="item in userMusicSource.mySubscribedPlaylist"
            class="appLeftMenuItem"
            :key="item.id"
            :class="{ menuActive: commonStore.activedMenu === String(item.id) }"
            @click="handleClickUserMusicSource(item.id)"
          >
            {{ item.name }}
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
  </div>
</template>

<script setup lang="ts">
import { watch, ref } from "vue";
import { useRouter } from "vue-router";
import useLoginStore from "@/stores/login";
import useCommonStore from "@/stores/common";
import useUserMusicSourceStore from "@/stores/userMusicSource";

const router = useRouter();
const activeName = ref(["1"]);
const commonStore = useCommonStore();
const loginStore = useLoginStore();
const userMusicSource = useUserMusicSourceStore();

const changeMenu = (menu: string, path?: string) => {
  commonStore.switchActivedMenu(menu);
  if (path) {
    router.push({
      path,
    });
  }
};

const handleCollapseChange = (val: string) => {
  console.log(val);
};

const gotoMyFavoList = (val: string) => {
  // myLikedPlaylist
  changeMenu(val);
  if (userMusicSource.myLikedPlaylist && userMusicSource.myLikedPlaylist[0]) {
    router.push({
      name: "albumAndSongSheet",
      query: {
        id: userMusicSource.myLikedPlaylist[0].id,
      },
    });
  }
};

const handleClickUserMusicSource = (id: number | string) => {
  changeMenu(String(id));
  commonStore.switchActivedMenu(String(id));
  router.push({
    name: "albumAndSongSheet",
    query: {
      id: id,
    },
  });
};

watch(
  () => loginStore.loginSuccess,
  (n, v) => {
    if (n === true) {
      if (loginStore.accountData && loginStore.accountData.id) {
        userMusicSource.queryUserPlaylist(loginStore.accountData.id);
      }
    }
  }
);
</script>

<style scoped lang="less">
.appLeftMenuCont {
  height: 100%;
  overflow-y: auto;
  width: 200px;
  padding: 10px;
  border-right: 1px solid rgb(224, 224, 224);
  .gap {
    color: #ccc;
    font-size: 14px;
    margin-top: 16px;
    margin-bottom: 8px;
  }
  .appLeftMenuItem {
    height: 36px;
    padding-left: 5px;
    line-height: 36px;
    color: rgb(53, 53, 53);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    &:hover {
      background-color: rgb(246, 246, 246);
    }
    &.menuActive {
      background-color: rgb(246, 246, 246);
      color: rgb(53, 53, 53);
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>
